/****************************************************************************
 * 2025.8 小庆永不失眠
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
    public partial class unlockedIconPanel : UIElement, IController
	{
		private Dictionary<string, System.Tuple<ExpUpgradeItem, Image>> mUnlockKeys = new Dictionary<string, System.Tuple<ExpUpgradeItem, Image>>();
		ResLoader mResLoader = ResLoader.Allocate();
		private void Awake()
		{
			UnlockIconTemplate.Hide();
			var iconAltas =  mResLoader.LoadSync<SpriteAtlas>("Icon");

            foreach (var expUpgradeItem in this.GetSystem<ExpUpgradeSystem>().Items)
            {
				var cacheItem = expUpgradeItem;
				expUpgradeItem.CurrentLevel.RegisterWithInitValue((level) =>
				{
					if(level>0)
					{
						if(mUnlockKeys.ContainsKey(cacheItem.Key))
						{

						}
						else
						{
							UnlockIconTemplate.InstantiateWithParent(UnlockIconRoot)
							.Self(self =>
							{
								self.sprite = iconAltas.GetSprite(cacheItem.IconName);
								mUnlockKeys.Add(cacheItem.Key, new System.Tuple<ExpUpgradeItem, Image>(cacheItem, self));

							}).Show();
						}
					}
				}).UnRegisterWhenGameObjectDestroyed(gameObject);
            }

			Global.Superknife.Register((unlocked)=>
			{
				if(unlocked)
				{
					if(mUnlockKeys.ContainsKey("simple_knife"))
					{
						var item = mUnlockKeys["simple_knife"].Item1;
						var sprite = iconAltas.GetSprite(item.PairedIconName);
						mResLoader.AddObjectForDestroyWhenRecycle2Cache(sprite);
						mUnlockKeys["simple_knife"].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);

            Global.SuperRotateSward.Register((unlocked) =>
            {
                if (unlocked)
                {
                    if (mUnlockKeys.ContainsKey("simple_rotate"))
                    {
                        var item = mUnlockKeys["simple_rotate"].Item1;
                        var sprite = iconAltas.GetSprite(item.PairedIconName);
                        mResLoader.AddObjectForDestroyWhenRecycle2Cache(sprite);
                        mUnlockKeys["simple_rotate"].Item2.sprite = sprite;
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.SuperBasketball.Register((unlocked) =>
            {
                if (unlocked)
                {
                    if (mUnlockKeys.ContainsKey("simple_basketball"))
                    {
                        var item = mUnlockKeys["simple_basketball"].Item1;
                        var sprite = iconAltas.GetSprite(item.PairedIconName);
                        mResLoader.AddObjectForDestroyWhenRecycle2Cache(sprite);
                        mUnlockKeys["simple_basketball"].Item2.sprite = sprite;
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);


            Global.SuperBomb.Register((unlocked) =>
            {
                if (unlocked)
                {
                    if (mUnlockKeys.ContainsKey("simple_bomb"))
                    {
                        var item = mUnlockKeys["simple_bomb"].Item1;
                        var sprite = iconAltas.GetSprite(item.PairedIconName);
                        mResLoader.AddObjectForDestroyWhenRecycle2Cache(sprite);
                        mUnlockKeys["simple_bomb"].Item2.sprite = sprite;
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);


            Global.SuperSward.Register((unlocked) =>
            {
                if (unlocked)
                {
                    if (mUnlockKeys.ContainsKey("simple_sword"))
                    {
                        var item = mUnlockKeys["simple_sword"].Item1;
                        var sprite = iconAltas.GetSprite(item.PairedIconName);
                        mResLoader.AddObjectForDestroyWhenRecycle2Cache(sprite);
                        mUnlockKeys["simple_sword"].Item2.sprite = sprite;
                    }
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;
        }
    }
}